﻿using Breeze.WebApi;
using LunchLearn.WebApi.Infrastructure.Notifications;
using LunchLearn.WebApi.Domain;
using LunchLearn.WebApi.Repository;

namespace LunchLearn.WebApi.Infrastructure.TrackingServices
{
    public class OrderLineTrackingService : IChangeTrackingService
    {
        private readonly INotificationBridge _messageBridge;

        public OrderLineTrackingService(INotificationBridge messageBridge)
        {
            _messageBridge = messageBridge;
        }

        public void Track(EntityInfo entityInfo, LunchLearnContext context)
        {
            var orderLine = entityInfo.Entity as OrderLine;
            context.Entry(orderLine).Reference(c => c.Order).CurrentValue = null;
            if (entityInfo.EntityState == EntityState.Added)
            {
                _messageBridge.SendNotification(new Notification(NotificationType.Added, orderLine, "An Order Line has been added to order {0} ", orderLine.OrderId));
            }
            if (entityInfo.EntityState == EntityState.Deleted)
            {
                _messageBridge.SendNotification(new Notification(NotificationType.Deleted, orderLine, "OrderLine {0} from order {1} has been deleted", orderLine.Id, orderLine.OrderId));
            }
        }

    }
}
